package com.lcp.utils;


import com.google.common.base.Charsets;
import com.lcp.MainApp;
import com.lcp.pojo.CadInfo;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CommonUtil {
    public static String getFilePath(){
        String jarWholePath = MainApp.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        try {
            jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8");
        } catch (UnsupportedEncodingException e) { System.out.println(e.toString()); }
        String jarPath = new File(jarWholePath).getParentFile().getAbsolutePath();
        return  jarPath;
    }

    public static String getUserDefaultPath(){
        javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView();
        return fsv.getDefaultDirectory().getAbsolutePath();
    }





    public static boolean createPath(String path){
        File dirFile = null;
        try {
            dirFile = new File(path);
            if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
                boolean creadok = dirFile.mkdirs();
                if (creadok) {
                    System.out.println("OK: 创建文件夹成功！ ");
                } else {
                    System.out.println("ERROR: 创建文件夹失败！ " + path);
                }
            }
            else{
                System.out.println("INFO: 文件夹已经存在！ ");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
            return false;
        }

        return true;

    }


    public static String getDXFName() {
        String res = "";
        String filePath = getFilePath();
        File file = new File(filePath);
        if (file.isDirectory()){
            List<File> fileList = Stream.of(file.listFiles()).filter(f -> f.getName().endsWith("dxf")).collect(Collectors.toList());
            if (fileList!=null&&fileList.size()>0){
                fileList.sort(Comparator.comparing(File::lastModified).reversed());
                return fileList.get(0).getName();
            }
        }
        return res;
    }

    public static String getLastFile() {
        return getUserDefaultPath()+File.separator+getDXFName();
    }


    public static String getDXFDATA() throws URISyntaxException {
        URL resource = CommonUtil.class.getClassLoader().getResource("cadinfo.data");
        Path path = Paths.get(resource.toURI());
        try {
            byte[] contents = Files.readAllBytes(path);
            String contentUtf8 = new String(contents, Charsets.UTF_8);
            CadInfo cadInfo = JsonUtils.jsonToPojo(contentUtf8, CadInfo.class);
            return JsonUtils.objectToJson(cadInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
